package io.github.cubelitblade.librarymanagementbackend.controller;

import io.github.cubelitblade.librarymanagementbackend.entity.Category;
import io.github.cubelitblade.librarymanagementbackend.service.CategoryService;
import io.github.cubelitblade.librarymanagementbackend.utils.ResultContext;
import io.github.cubelitblade.librarymanagementbackend.vo.CategoryWithBooksVO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/categories")
public class CategoryController {
    private final CategoryService categoryService;
    public CategoryController(CategoryService categoryService){this.categoryService = categoryService;}

    @GetMapping
    public ResponseEntity<ResultContext<List<Category>>> getAllCategories(){
        return ResponseEntity.ok(ResultContext.succeed(categoryService.getAllCategories()));
    }

    @GetMapping("/books")
    public ResponseEntity<ResultContext<CategoryWithBooksVO>> getAllBooks(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ){
        CategoryWithBooksVO result = categoryService.getAllBooks(page, size);
        return ResponseEntity.ok(ResultContext.succeed(result));
    }

    @GetMapping("/{categoryId}")
    public ResponseEntity<ResultContext<CategoryWithBooksVO>> getCategoryWithBooks(
            @PathVariable Integer categoryId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        CategoryWithBooksVO result = categoryService.getCategoryWithBooks(categoryId, page, size);
        return ResponseEntity.ok(ResultContext.succeed(result));
    }

    @GetMapping("/name")
    public ResponseEntity<ResultContext<String>> getCategoryNameById(@RequestParam Integer categoryId) {
        String name = categoryService.getCategoryNameById(categoryId);
        return ResponseEntity.ok(ResultContext.succeed(name));
    }
}
